package com.chen.bpsptsystem.common;

import java.util.ArrayList;
import java.util.List;

/**
 * @CLASSNAME TreeParser
 * @DESCRIPTION 描述
 * @AUTHOR chen
 * @DATE 2018-10-15 17:53
 **/
public class TreeParser {

    public static <E extends TreeEntity <E>> List <E> getTreeList(Integer topId, List <E> entityList) {
        List <E> resultList = new ArrayList <>();
        //获取顶层元素集合
        Integer parentId;
        for (E entity : entityList) {
            parentId = entity.getParentId();
            if (parentId == null || topId.equals( parentId )) {
                resultList.add( entity );
            }
        }
        //获取每个顶层元素的子数据集合
        for (E entity : resultList) {

            entity.setChildList( getSubList( entity.getCategoryId(), entityList ) );
//            entity.setChildList( getSubList( entity.getId(), entityList ) );
        }
        return resultList;
    }

    private static <E extends TreeEntity <E>> List <E> getSubList(Integer id, List <E> entityList) {
        List <E> childList = new ArrayList <>();
        Integer parentId;

        //子集的直接子对象
        for (E entity : entityList) {
            parentId = entity.getParentId();
            if (id.equals( parentId )) {
                childList.add( entity );
            }
        }
        //子集的间接子对象
        for (E entity : childList) {
            entity.setChildList( getSubList( entity.getCategoryId(), entityList ) );
//            entity.setChildList( getSubList( entity.getId(), entityList ) );
        }
        //递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }

}
